package com.adxon.data2;

import com.adxon.MsgException;
import com.google.appengine.api.images.*;
import com.google.appengine.api.images.InputSettings.OrientationCorrection;

public class Figure extends Bits{
	protected static OutputSettings jpeg(){
		return jpeg(90);
	}
	protected static OutputSettings jpeg(int quality){
		OutputSettings re=new OutputSettings(ImagesService.OutputEncoding.JPEG);
		re.setQuality(quality);
		return re;
	}
	protected static OutputSettings png(){
		return new OutputSettings(ImagesService.OutputEncoding.PNG);
	}

	private String ms;		// max-size
	private String rt;		// ratio

	@Override
	public void commit()throws MsgException{
		if(bo!=null){
			if(ms!=null || rt!=null){
				ImagesService s=ImagesServiceFactory.getImagesService();
				Image m=ImagesServiceFactory.makeImage(bo);
				InputSettings i=new InputSettings();
				i.setOrientationCorrection(OrientationCorrection.CORRECT_ORIENTATION);
				Transform z=ImagesServiceFactory.makeCrop(0,0,1,1);
				try{
					m=s.applyTransform(z,m,i,png());	// correct orientation
				}catch(IllegalArgumentException e){
				}
				int w=m.getWidth();
				int h=m.getHeight();
				if(rt!=null){
					float a=(float)w/h,k=(float)3/4;
					System.out.println("ratio = "+rt+'('+a+'/'+k+')');
					float l=0,t=0,r=1,b=1;
					if(a>=k){
						r=k*h/w;
					}else{
						b=w/(k*h);
					}
					z=ImagesServiceFactory.makeCrop(l,t,r,b);
					m=s.applyTransform(z,m);
				}
				z=ImagesServiceFactory.makeResize(1536,1536);
				m=s.applyTransform(z,m,jpeg());
				bo=m.getImageData();
				tm=0;								// set modified flag
			}
		}
		super.commit();
	}
	@Override
	public byte[] getBits(String type)throws MsgException{
		byte[] r;
		if(type==null)
			r=getBitsD();
		else if(type.equalsIgnoreCase("o"))
			return getBits();
		else if(type.equalsIgnoreCase("s"))
			r=getBitsS();
		else if(type.equalsIgnoreCase("t"))
			r=getBitsT();
		else r=getBitsD();
		commit();
		return r;
	}
	private byte[] getBitsD(){
		bd=getBits("bd",bd);
		if(bd==null){
			byte[] b=getBits("bo",bo);
			if(b!=null){
				Image m=ImagesServiceFactory.makeImage(b);
				Transform z=ImagesServiceFactory.makeResize(768,768);
				m=ImagesServiceFactory.getImagesService().applyTransform(z,m,jpeg());
				bd=m.getImageData();
				tm=0;		// set modified flag
			}
		}
		return bd;
	}
	private byte[] getBitsS(){
		bm=getBits("bm",bm);
		if(bm==null){
			byte[] b=getBits("bo",bo);
			if(b!=null){
				Image m=ImagesServiceFactory.makeImage(b);
				Transform z=ImagesServiceFactory.makeResize(512,512);
				m=ImagesServiceFactory.getImagesService().applyTransform(z,m,jpeg());
				bm=m.getImageData();
				tm=0;		// set modified flag
			}
		}
		return bm;
	}
	private byte[] getBitsT(){
		bt=getBits("bt",bt);
		if(bt==null){
			byte[] o=getBits("bo",bo);
			if(o!=null){
				Image m=ImagesServiceFactory.makeImage(o);
				float w=m.getWidth();
				float h=m.getHeight();
				float l,t,r,b;
				int k;
				if(w>=h){
					k=(int)(w*96/h);
					l=0;
					r=h/w;
					t=0;
					b=1;
				}else{
					k=(int)(h*96/w);
					l=0;
					r=1;
					t=0;
					b=w/h;
				}
				Transform z=ImagesServiceFactory.makeResize(k,k);
				m=ImagesServiceFactory.getImagesService().applyTransform(z,m);
				z=ImagesServiceFactory.makeCrop(l,t,r,b);
				m=ImagesServiceFactory.getImagesService().applyTransform(z,m,jpeg());
				bt=m.getImageData();
				tm=0;		// set modified flag
			}
		}
		return bt;
	}
	public String getExtension(){
		return ".jpg";
	}
	public void setMaxSize(String size){
		if(size!=null)
			ms=size;
	}
	public void setRatio(String ratio){
		if(ratio!=null)
			rt=ratio;
	}
}